package com.wcgksoft.controller.dataShow;

import com.alibaba.fastjson.JSONObject;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.ZData.BasicData;
import com.wcgksoft.entity.dataAnalysis.DepartProjectData;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.BasicDataService;
import com.wcgksoft.service.DataAnalysis.DepartProjectDataService;
import com.wcgksoft.service.DataAnalysis.ThresholdService;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.Equipment.DataTypeService;
import com.wcgksoft.service.Equipment.EquipmentService;
import com.wcgksoft.service.system.UserService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName DataShowController
 * @Description 数据展示
 * @Author 王宁
 * @Date 2020/3/30 9:36
 * @Version 1.0
 **/
@Controller
@RequestMapping("dataShow")
public class DataShowController {

  @Autowired
  private DataTypeService dataTypeService;

  @Autowired
  private EquipmentService equipmentService;

  @Autowired
  private DepartProjectDataService departProjectDataService;

  @Autowired
  private EnterpriseProjectService enterpriseProjectService;

  @Autowired
  private ThresholdService thresholdService;
  @Autowired
  private BasicDataService basicDataService;
  @Autowired
  private UserService userService;


  @RequestMapping("count")
  public String count(ModelMap modelMap, HttpServletRequest req, @RequestParam(value = "project", required = false) Integer id) {
    EnterpriseProject id1 = enterpriseProjectService.findById(id);
    req.getSession().setAttribute("project", id1);
    return "dataShow/dataShow";
  }

  @ResponseBody
  @RequestMapping(value = "gettree")
  public List<EnterpriseProject> gettree(HttpServletRequest request, @RequestParam(value = "userid", required = false) Integer userid) {

    SysUser sysUser = userService.findById(userid);
    DetachedCriteria ent = DetachedCriteria.forClass(EnterpriseProject.class);
    ent.add(Restrictions.eq("enterprise.id", sysUser.getEnterprise().getId()));
    List<EnterpriseProject> byConditions = enterpriseProjectService.findByConditions(ent);
    for (EnterpriseProject byCondition : byConditions) {
      DetachedCriteria detachedCriteria = DetachedCriteria.forClass(DepartProjectData.class);
      detachedCriteria.add(Restrictions.eq("bbbbid", byCondition.getId()));
      List<DepartProjectData> list = departProjectDataService.findByConditions(detachedCriteria);
      byCondition.setDepartProjectData(list);
    }
    return byConditions;

  }


  @ResponseBody
  @RequestMapping(value = "ge")
  public JSONObject ge(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id) {
    DepartProjectData byId = departProjectDataService.findById(id);
    DetachedCriteria criteria = DetachedCriteria.forClass(BasicData.class);
    criteria.add(Restrictions.eq("com", byId.getCom()));
    criteria.add(Restrictions.eq("no", byId.getEquipment().getE_number()));
    criteria.add(Restrictions.eq("datatype", byId.getDataType().getD_key()));
    List<Date> listdate = new ArrayList<>();
    List<String> listdata = new ArrayList<>();
    List<BasicData> data = basicDataService.findByConditions(criteria);
    for (BasicData datum : data) {
      listdate.add(datum.getAcquisitiontime());
      listdata.add(datum.getData());
    }
    JSONObject object = new JSONObject();
    object.put("data", listdata);
    object.put("time", listdate);
    object.put("uity", byId.getDataType().getD_uity());
    object.put("ifse", byId.getDataType().getD_key());
    object.put("title", byId.getDataName());
    return object;
  }

  /**
   * @throws
   * @title TODO
   * @description TODO
   * @author 阿良
   * @param: id
   * @param: page
   * @param: limit
   * @updateTime 2020/4/6 12:25
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "findAll")
  public Layui findAll(@RequestParam(value = "id") Integer id,
                       @RequestParam(value = "page", required = false) Integer page,
                       @RequestParam(value = "limit", required = false) Integer limit) {
    DepartProjectData byId = departProjectDataService.findById(id);
    DetachedCriteria criteria = DetachedCriteria.forClass(BasicData.class);
    criteria.add(Restrictions.eq("com", byId.getCom()));
    criteria.add(Restrictions.eq("no", byId.getEquipment().getE_number()));
    criteria.add(Restrictions.eq("datatype", byId.getDataType().getD_key()));
    criteria.addOrder(Order.desc("Acquisitiontime"));
    List<BasicData> data = basicDataService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", basicDataService.getTotalCount(), data);
  }
}
